<template>
  <div class="shop-control w-card">
    <div class="title-box">
      <span class="title">商户活动价配置</span>
      <span class="tips">(配置上的商户，活动的价格允许低于成本价)</span>
      <el-button style="margin-left:auto;" type="primary" icon="Plus" @click="addShop">添加商户</el-button>
    </div>
    <div class="table-box">
      <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="shopId" label="商户标识" align="center">
          <template v-slot="scope">
            {{ scope.row.shopId || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="shopName" label="商户名称" align="center">
          <template v-slot="scope">
            {{ scope.row.shopName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="operations" label="操作" align="center" width="120">
          <template v-slot="scope">
            <el-link style="font-size:14px;" type="danger" @click="setHandle('del',scope.row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <add-shop ref="addShopRef" @dialogClose="addShopClose" />
    <confirm-pass ref="confirmPassRef" @verification="verification" />
  </div>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { getWindowWidthAndHeight, getDateTime, warningDeal, successDeal } from '@/utils/utils'
import api from './api'
import addShop from './components/addShop.vue'
import confirmPass from '@/components/confirmPass.vue'
export default {
  components: {
    addShop,
    confirmPass,
  },
  setup() {
    const addShopRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      handleType: null,
      handleData: null,

      tableHeight: 0,
      tableData: [],
    })

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 200
      getTableData()
    })

    function getTableData() {
      const obj = {}
      api.goodsActAllowList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = (res.data?.list || [])
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function setHandle(type, data) {
      state.handleType = type
      state.handleData = data

      let title = ''
      let msg = ''
      switch (type) {
        case 'del':
          // 删除
          title = '提示'
          msg = '确定要删除该商户吗？'
          confirmPassRef.value.dialogShow(title, msg, false)
          break
      }
    }

    function verification() {
      deleteHandle(state.handleData?.id)
    }

    function deleteHandle(id = null) {
      if (!id) return
      const obj = {
        id
      }
      api.goodsActAllowDelete(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addShopClose() {
      getTableData()
    }

    function addShop() {
      addShopRef.value.dialogShow()
    }

    return {
      ...toRefs(state),
      addShopRef,
      confirmPassRef,
      setHandle,
      getDateTime,
      addShopClose,
      addShop,
      verification,
    }
  }
}
</script>

<style lang="scss" scoped>
.shop-control {
  width: 100%;
  height: 100%;
  .title-box {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding: 20px;
    > .title {
      font-size: 16px;
      font-weight: bold;
    }
    > .tips {
      font-size: 14px;
      color: #707070;
    }
  }
  .table-box {
    box-sizing: border-box;
    padding: 20px;
    padding-top: 0;
  }
}
</style>